package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Apply_invoiceRequest;
import com.xm.xmapp.request.bean.Get_user_invoice_infoRequest;
import com.xm.xmapp.response.bean.Get_user_invoice_infoResponse;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.tools.Tools;

public class InvoiceActivity extends BaseActivity implements OnClickListener {

	private ImageView back, img1, img2;
	private TextView title, maxmoney, right_title;
	private EditText money, company, address;
	private LinearLayout lin1, lin2;
	private TextView fapiao, text_money;
	private ResponseUserMoney bean;
	private Get_user_invoice_infoResponse responseBean;
	private Apply_invoiceRequest requestBean = new Apply_invoiceRequest();
	/**
	 * flag个人还是单位
	 */
	private int flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invoice);
	}

	@Override
	public void initView() {

		back = (ImageView) findViewById(R.id.back);
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		title = (TextView) findViewById(R.id.title);
		fapiao = (TextView) findViewById(R.id.fapiao);
		lin1 = (LinearLayout) findViewById(R.id.lin1);
		lin2 = (LinearLayout) findViewById(R.id.lin2);
		maxmoney = (TextView) findViewById(R.id.maxmoney);
		money = (EditText) findViewById(R.id.money);
		company = (EditText) findViewById(R.id.company);
		address = (EditText) findViewById(R.id.address);
		text_money = (TextView) findViewById(R.id.text_money);
		right_title = (TextView) findViewById(R.id.right_title);
	}

	@Override
	public void initData() {

		title.setText("申请发票");
		flag = 0;
		right_title.setVisibility(View.VISIBLE);
		right_title.setText("历史记录");
		bean = (ResponseUserMoney) getIntent().getSerializableExtra("bean");
		if (bean != null && bean.user_money != 0) {
			maxmoney.setText(bean.user_money + "");
		} else {
			fapiao.setText("尚未达到可开发票的条件");
			fapiao.setClickable(false);
			fapiao.setBackgroundResource(R.drawable.myborder4);
		}
		getData();

	}

	private void getData() {
		Get_user_invoice_infoRequest requestBean = new Get_user_invoice_infoRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				responseBean = JSON.parseObject(response, Get_user_invoice_infoResponse.class);
				if (responseBean.invoice_min_money != null && !responseBean.invoice_min_money.equals("")) {
					text_money.setText("1.发票金额必须达到额定的金额，发票的最小面值是" + responseBean.invoice_min_money
							+ "元\n2.金额满500元可免快递费用\n3.官方默认快递是润达快递，五日内发出");
					if (bean == null || bean.user_money <= 0) {
						fapiao.setText("尚未达到可开发票的条件");
						fapiao.setClickable(false);
						fapiao.setBackgroundResource(R.drawable.myborder4);
					} else {
						if (bean.user_money < Double.parseDouble(responseBean.invoice_min_money)) {
							fapiao.setText("尚未达到可开发票的条件");
							fapiao.setClickable(false);
							fapiao.setBackgroundResource(R.drawable.myborder4);
						}
					}

				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void initEvent() {

		lin1.setOnClickListener(this);
		lin2.setOnClickListener(this);
		back.setOnClickListener(this);
		fapiao.setOnClickListener(this);
		right_title.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {
		case R.id.fapiao:
			if (responseBean != null) {
				applyInvoice();
			} else {
				getData();
			}
			break;
		case R.id.back:
			finish();
			break;
		case R.id.lin1:
			if (flag != 0) {
				flag = 0;
				setImg();
			}
			break;

		case R.id.lin2:
			if (flag != 1) {
				flag = 1;
				setImg();
			}
			break;
		case R.id.right_title:

			intent = new Intent(MyAppcation.getMyAppcation(), InvoiceListActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	private void applyInvoice() {
		if (money.getText().toString().equals(""))
			Tools.showMessage("报销金额不能为空");
		else if (company.getText().toString().equals(""))
			Tools.showMessage("发票抬头不能为空");
		else if (address.getText().toString().equals(""))
			Tools.showMessage("地址不能为空");
		else {

			if (Double.parseDouble(money.getText().toString()) >= Double.parseDouble(responseBean.invoice_min_money)) {
				requestBean.invoice_address = address.getText().toString();
				requestBean.is_company_invoice = (flag == 0 ? 1 : 0);
				requestBean.invoice_money = money.getText().toString();
				requestBean.invoice_title = company.getText().toString();
				requestBean.consignee_phone = "13632905779";
				requestBean.consignee_name = "廖德威";
				promptDialog.show();
				MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

					@Override
					public void onResponse(String response) {
						promptDialog.dismiss();
						finish();

					}
				}, new DisssmissInterFace() {

					@Override
					public void putError(VolleyError error) {
						promptDialog.dismiss();
					}
				});

			} else {
				Tools.showMessage("最小发票金额为" + responseBean.invoice_min_money);
			}
		}

	}

	private void setImg() {

		if (flag == 0) {
			img1.setImageResource(R.mipmap.icon_xuanze_nor);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		} else {
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_nor);
		}
	}

}
